package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类
 *
 * @Author Yang
 * @Date 2023/3/2 11:43
 */
@Slf4j
@RestController
@Api(tags = "菜品分类")
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    List<Category> list = null;

    //查询所有
    @ApiOperation(value = "查询所有分类")
    @GetMapping("/findAll")
    public ResultInfo findAll(){

        //先查询缓存
        String categoryKey = "category_list";

        if (redisTemplate.hasKey(categoryKey)) {
            // yes redis有数据
            list = (List<Category>) redisTemplate.opsForValue().get(categoryKey);
            log.info("查询redis缓存");
        }else {
            // no redis没有数据
            // 调用service查询MySQL
            list = categoryService.findAll();
            log.info("查询MySQL数据库");
            redisTemplate.opsForValue().set(categoryKey,list);
        }


        return ResultInfo.success(list);
    }

    //新增
    @ApiOperation(value = "新增分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类对象", required = true),
    })
    @PostMapping
    public ResultInfo save(@RequestBody Category category){
        categoryService.save(category);

        // 清理缓存
        redisTemplate.delete("category_list");

        return ResultInfo.success();
    }

    //修改
    @ApiOperation(value = "修改分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category",value = "分类对象", required = true),
    })
    @PutMapping
    public ResultInfo update(@RequestBody Category category){
        categoryService.update(category);

        // 清理缓存
        redisTemplate.delete("category_list");

        return ResultInfo.success();
    }

    //删除
    @ApiOperation(value = "删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类id", required = true),
    })
    @DeleteMapping
    public ResultInfo deleteById(Long id){
        categoryService.deleteById(id);

        // 清理缓存
        redisTemplate.delete("category_list");

        return ResultInfo.success();
    }

    //查询菜品分类
    @ApiOperation(value = "查询菜品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "分类类型 1 菜品分类 2 套餐分类", required = true),
    })
    @GetMapping("/list")
    public ResultInfo list(Integer type){
        List<Category> categories = categoryService.list(type);
        return ResultInfo.success(categories);
    }
}
